"""
获取一个向量的均值和方差

1.生成假老师
2.生成高斯平滑
"""
import numpy as np
import torch
import math

"""
假老师
"""
def Fake_Teacher(batch):
    test_set = batch.cpu().numpy()
    fake_teacher = []

    for set in test_set:
        mean = np.mean(set)
        var = np.var(set)
        fake = np.random.normal(mean, var, 10)
        fake = fake.astype('float32')
        fake_teacher.append(fake)
    fake_teacher = torch.tensor(fake_teacher)
    return fake_teacher


"""
高斯滤波器
"""
def GetGaussFilter(sigma, delta, label):
    """
    返回峰值在label位置的高斯向量
    :param sigma:均值
    :param delta:方差
    :param label:
    :return:向量
    """
    left = 1 / (np.sqrt(2 * math.pi) * np.sqrt(delta))
    right = np.exp(-(label - sigma) ** 2 / (2 * delta))
    return left * right

def GetLabelSmoothFilter(label, var):
    delta = 324 -(4 * 90 * (0.9 - 10 * var))
    x = (18 - np.sqrt(delta)) / 180
    smoothlabel = []
    for l in range(10):
        if l == label:
            smoothlabel.append(1 - 9 * x)
        else:
            smoothlabel.append(x)
    _var = np.var(smoothlabel)
    smoothlabel = np.array(smoothlabel).astype('float32')
    return smoothlabel


"""
产生高斯label smoothing
"""
def GetGaussTeacher(batch, labels):
    test_set = batch.cpu().numpy()
    Gauss_teacher = []

    for index, set in enumerate(test_set):
        mean = list(set).index(max(set))
        var = np.var(set)
        _label = labels[index].item()
        g_teacher = []
        # for l in range(10):
        #     gaussSmLb = GetGaussFilter(mean, var, l)
        #     g_teacher.append(gaussSmLb)
        g_teacher = GetLabelSmoothFilter(mean, var)
        Gauss_teacher.append(g_teacher)
    Gauss_teacher = torch.tensor(Gauss_teacher)
    return Gauss_teacher